﻿using Microsoft.AspNetCore.Mvc.ModelBinding;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wq.Extensions.Common;
using Wq.Extensions.Repository;
using Wq.MeetingService.Entities;
using Wq.MeetingService.Models;
using Wq.MeetingService.Models.Enum;
using Wq.Model;

namespace Wq.MeetingService
{
    public class DiningRommService : SqlSugarBaseRepository<DiningRommInfo>, IAddSingleton
    {
        /// <summary>
        /// 验证餐厅
        /// </summary>
        /// <param name="model"></param>
        private void Verify(DiningRommEdit model)
        {
            if (string.IsNullOrWhiteSpace(model.Name))
                throw new MyException("餐厅名称不能为空");
            if (model.VenueId == 0)
                throw new MyException("请选择场地");
            if (model.People <= 0)
                throw new MyException("容纳人数不能为空");
            if (model.Area <= 0)
                throw new MyException("面积不能为空");
            if (model.Images.Count == 0)
                throw new MyException("请上传图片");

        }
        /// <summary>
        /// 编辑模型转实体
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        private DiningRommInfo EditToModel(DiningRommEdit info)
        {
            var model = info.Map<DiningRommInfo>();
            model.Thumbnails = info.Images.Select(p => new MeetingThumbnailInfo { VenueId = info.VenueId, TypeId = PositionType.Guest, FilePath = p }).ToList();
            return model;
        }

        /// <summary>
        /// 创建客房
        /// </summary>
        /// <param name="info"></param>
        public void Create(DiningRommEdit info)
        {
            Verify(info);
            var model = EditToModel(info);
            Context.Ado.UseTran(() => {
                Context.InsertNav(model)
               .Include(p => p.Thumbnails)
               .ExecuteCommand();
                UpdateVenue(info.VenueId);
            });
        }

        /// <summary>
        /// 编辑客房
        /// </summary>
        /// <param name="info"></param>
        public void Save(DiningRommEdit info)
        {
            Verify(info);
            var model = EditToModel(info);
            Context.Ado.UseTran(() => {
                Context.UpdateNav(model)
              .Include(p => p.Thumbnails)
              .ExecuteCommand();
                UpdateVenue(info.VenueId);
            });
        }

        /// <summary>
        /// 会议会议室统计字段
        /// </summary>
        /// <param name="venueId"></param>
        public void UpdateVenue(long venueId)
        {
            var count = Context.Queryable<DiningRommInfo>().Where(p => p.VenueId == venueId).Count();
            Context.Updateable<VenueInfo>()
                .SetColumns(p => p.DiningRoomCount == count)
                .Where(p => p.Id == venueId).ExecuteCommand();
        }

        /// <summary>
        /// 修改排序
        /// </summary>
        /// <param name="id"></param>
        /// <param name="sequence"></param>
        public void UpdateSequence(long id, long sequence) => Context.Updateable<DiningRommInfo>().SetColumns(p => p.Sequence == sequence).Where(p => p.Id == id).ExecuteCommand();


        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public DiningRommInfo Details(long id) =>
            Context.Queryable<DiningRommInfo>()
            .Where(p => p.Id == id)
            .Includes(p => p.Thumbnails)
            .Includes(p => p.PrivateRooms)
            .First();

    }

}
